package com.talent.web.config.security.phone;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import java.util.Collection;

/**
 * @ClassName：SmsCodeAuthenticationToken
 * @Author: hszhou
 * @Date: 2025/2/28 14:06
 * @Description: 创建手机验证码认证令牌
 */
public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {
    /**手机号*/
    private final Object principal;
    /**验证码*/
    private Object credentials;
    public SmsCodeAuthenticationToken(Object principal, Object credentials) {
        super(null);
        this.principal = principal;
        this.credentials = credentials;
        this.setAuthenticated(false);
    }

    public SmsCodeAuthenticationToken(Object principal, Object credentials,Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credentials = credentials;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return credentials;
    }

    @Override
    public Object getPrincipal() {
        return principal;
    }

    /**
     * 不允许通过set方法设置认证标识
     * @param authenticated w
     */
    @Override
    public void setAuthenticated(boolean authenticated) {
        if (authenticated) {
            throw new IllegalArgumentException(
                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }
        super.setAuthenticated(false);
    }
    /**
     * 擦除登录凭据
     */

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
        credentials = null;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return super.getAuthorities();
    }

    /**
     * 获取认证token的名字
     */
    @Override
    public String getName() {
        return "mobilePhoneAuthenticationToken";
    }

}